package com.tappytaps.android.hc2010.helper;

import java.util.Locale;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;
//import com.tappytaps.android.hc2010.helper.Translate;

public class TranslatorHelper implements TextToSpeech.OnInitListener{
	Language source;
	Language destination;
	private TextToSpeech textToSpeech;
	public TranslatorHelper(Language sourceLanguage, Language destinationLanguage, Context context) {
		this.source = sourceLanguage;
		this.destination = destinationLanguage;
		Translate.setHttpReferrer("http://www.tappytaps.com");
		 textToSpeech = new TextToSpeech(context, this);
	}
	
		
	private String translate(String text, Language source, Language destination) {
		String ret = null;
		try {
			ret = Translate.execute(text,source, destination);
			//ret = Translate.translate(text, "en", "fr");
		} catch (Exception e) {
			Log.i("TRANSLATION", e.toString());
		}
		return ret;		
	}
	
	public String translateFromSource(String text) {
		return translate(text, source, destination);
	}
	
	public String translateFromDestination(String text) {
		return translate(text, destination, source);
	}
	
	public void say(String text, Language lan) {
		int ret = textToSpeech.setLanguage(new Locale(lan.toString()));
		
		textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
            null);
	}

	public void sayInSource(String text) {
		say(text, source);
	}

	public void sayInDestination(String text) {
		say(text, destination);
	}

	public void onInit(int status) {
		//int ret = textToSpeech.setLanguage(new Locale(destination.name()));
		/*
		Log.i("TRANSLATE", "Selected language: "+destination.toString());
		int ret = textToSpeech.setLanguage(new Locale(destination.toString()));
		Log.i("TRANSLATE", new Integer(ret).toString());
		*/
	}
	
}
